home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / VALUE.C < prev    next >
C/C++ Source or Header  |  1992-08-20  |  4KB  |  152 lines

  1. //
  2. // Copyright (C) 1992 General Electric Company.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // General Electric Company provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Changed: VDN 04/15/92 -- Lice version
  13. //
  14. // Value is a general slot capable of taking void* and all primitive types.
  15. // It is needed to store any value in a property list, and to do conversion.
  16.  
  17.  
  18. #include <cool/Value.h>
  19.  
  20. static char* CoolValue_Type_Names_s[] = 
  21. {
  22.   "Unknown",                    // formatted output
  23.   "char",
  24.   "int",
  25.   "unsigned",
  26.   "long",
  27.   "float",
  28.   "double",
  29.   "void*"
  30. };
  31.  
  32. static int CoolValue_Type_Sizes_s[] = 
  33. {
  34.   0,                        // sizes in bytes for copying
  35.   sizeof(char),
  36.   sizeof(int),
  37.   sizeof(unsigned),
  38.   sizeof(long),
  39.   sizeof(float),
  40.   sizeof(double),
  41.   sizeof(void*)
  42. };
  43.  
  44. // type_error()  -- Raise exception for unmatched type at runtime.
  45. // Input:    function name, and 2 types.
  46. // Output:   exception or abort.
  47.  
  48. void CoolValue::type_error(const char* fcn, Type t1, Type t2) const {
  49.   //RAISE (Error, SYM(CoolValue), SYM(Invalid_Type),
  50.   printf ("CoolValue::%s(): Type %s and %s do not match.\n", 
  51.       fcn, CoolValue_Type_Names_s[t1], CoolValue_Type_Names_s[t2]);
  52.   abort ();
  53. }
  54.  
  55. // CoolValue()  -- Empty constructor.
  56. // Input:   none
  57. // Output:  Value reference
  58.  
  59. CoolValue::CoolValue () 
  60. : type(UNKNOWN) {              
  61.   for (int i = 0; i < sizeof(this->data); i++)    // zero out all bytes
  62.     this->data[i] = 0;
  63. }
  64.  
  65.  
  66. // CoolValue()  -- Copy constructor.
  67. // Input:   Value to be copied
  68. // Output:  Value reference
  69.  
  70. CoolValue::CoolValue (const CoolValue& v) 
  71. : type(v.type) {
  72.   for (int i = 0; i < CoolValue_Type_Sizes_s[v.type]; i++) // copy all bytes
  73.     this->data[i] = v.data[i];
  74. }
  75.  
  76. // operator =   -- Assignment right to left values, no type-checking.
  77. // Input:   left and right values
  78. // Output:  right value reference
  79.  
  80. CoolValue& CoolValue::operator= (const CoolValue& v) {
  81.   this->type = v.type;
  82.   for (int i = 0; i < CoolValue_Type_Sizes_s[v.type]; i++) // copy all bytes
  83.     this->data[i] = v.data[i];
  84.   return *this;
  85. }
  86.  
  87. // operator ==  -- Test for equality of two values.
  88. // Input:   this and second value
  89. // Output:  TRUE/FALSE for equality or not
  90.  
  91. Boolean CoolValue::operator== (const CoolValue& v) const {
  92.   if (this->type != v.type)            // check stored type
  93.     return FALSE;
  94.   switch (this->type) {                // base on stored type
  95.   case UNKNOWN:                    // do equality test
  96.     return TRUE;
  97.   case CHAR:                
  98.     return *((char*) this->data) == *((char*) v.data);
  99.   case INT:
  100.     return *((int*) this->data) == *((int*) v.data);
  101.   case UNSIGNED:
  102.     return *((unsigned*) this->data) == *((unsigned*) v.data);
  103.   case LONG:
  104.     return *((long*) this->data) == *((long*) v.data);
  105.   case FLOAT:
  106.     return *((float*) this->data) == *((float*) v.data); // == with machine fuzz
  107.   case DOUBLE:
  108.     return *((double*) this->data) == *((double*) v.data); 
  109.   case VOIDP:
  110.     return *((void**) this->data) == *((void**) v.data);
  111.   default:
  112.     return FALSE;
  113.   }
  114. }
  115.  
  116. // operator<<  -- Output value to ostream
  117. // Input:    ostream, reference to value
  118. // Output:   ostream reference
  119.  
  120. ostream& operator<< (ostream& os, const CoolValue& v) {
  121.   os << '(' << CoolValue_Type_Names_s[v.type] << ')';
  122.   switch (v.type) {                // base on stored type
  123.   case CoolValue::CHAR:                // do correct printout 
  124.     os << *((char*) v.data);            // of the bits.
  125.     break;
  126.   case CoolValue::INT:
  127.     os << *((int*) v.data);
  128.     break;
  129.   case CoolValue::UNSIGNED:
  130.     os << *((unsigned*) v.data);
  131.     break;
  132.   case CoolValue::LONG:
  133.     os << *((long*) v.data);
  134.     break;
  135.   case CoolValue::FLOAT:
  136.     os << *((float*) v.data);
  137.     break;
  138.   case CoolValue::DOUBLE:
  139.     os << *((double*) v.data);
  140.     break;
  141.   case CoolValue::VOIDP:
  142.     os << *((void**) v.data);
  143.     break;
  144.   case CoolValue::UNKNOWN:    
  145.   default:
  146.     os << "Undefined";
  147.     break;
  148.   }
  149.   return os;
  150. }
  151.  
  152.